﻿//////////////////////////////////////////////////////////////////////////////
//系统名：DOORSAAS系统
//模块名：系统日志操作类 
//模块说明： 系统日志操作类
//开发者：吴晶
//开发时间：2008年08月13日
//版本：1.0
//修改者：
//修改时间：
//////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Data.SqlClient;
using Sale_Common;
using Sale_DAL;

namespace Sale_Operation
{
    public class SystemLogOp
    {

        public SystemLogOp()
        {
        }
        public static void Add(Sale_Model.SystemLog _SysteLog)
        {
            SystemLogOp.Add((ESysLogType)int.Parse(_SysteLog.SLog_Type.ToString()), _SysteLog.SLog_Content, _SysteLog.SLog_Modu_ID);
        }
        /// <summary>
        /// 暂未实现
        /// </summary>
        /// <param name="eNum"></param>
        /// <param name="SLog_Content"></param>
        /// <param name="SLog_Modu_ID"></param>
        public static void Add(ESysLogType eNum, string SLog_Content, int? SLog_Modu_ID)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("INSERT INTO SystemLog (SLog_Type,SLog_Content,SLog_Modu_ID,SLog_User_ID,SLog_Date,CreateMan,CreateTime,State) SELECT ");
                sbSql.Append("SLog_Type=@SLog_Type,");
                sbSql.Append("SLog_Content=@SLog_Content,");
                sbSql.Append("SLog_Modu_ID=@SLog_Modu_ID,");
                sbSql.Append("SLog_User_ID=@SLog_User_ID,");
                sbSql.Append("SLog_Date=@SLog_Date,");
                sbSql.Append("CreateMan=@CreateMan,");
                sbSql.Append("CreateTime=@CreateTime,");
                sbSql.Append("State=@State");
                //如果传入模块ID为0，则是后台操作的错误日志信息，为满足数据库外键要求，转化为1，
                if (SLog_Modu_ID == 0)
                    SLog_Modu_ID = 1;
                SqlParameter[] para = new SqlParameter[] {
                    new SqlParameter("@SLog_Type", (int)eNum), 
                    new SqlParameter("@SLog_Content", SLog_Content.Length > 4000 ? SLog_Content.Substring(0, 4000) : SLog_Content), 
                    new SqlParameter("@SLog_Modu_ID", SLog_Modu_ID),
                    new SqlParameter("@SLog_User_ID", LinkService.GetCurrentUser().SUser_Id),
                    new SqlParameter("@SLog_Date",System.DateTime.Now), 
                    new SqlParameter("@CreateMan",LinkService.GetCurrentUser().SUser_Id),
                    new SqlParameter("@CreateTime",System.DateTime.Now),
                    new SqlParameter("@State", "0") 
                };

                db.Open();
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString(), para);
            }
            catch (Exception ex)
            {
            }
            db.Close();
        }
        public DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SLog_ID,SUser_Name,SLog_Type,SMenu_Title,SLog_Content,SLog_Modu_ID,SLog_Content,SLog_User_ID,SLog_Date FROM SystemLog A LEFT JOIN SysMenu B ON (A.SLog_Modu_ID=B.SMenu_Id) LEFT JOIN SysUser C ON (C.SUser_Id = A.SLog_User_ID)"
                            ); 
                if (strCondition != "")
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日志获取出错]:" + ex.ToString(), 0);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 导出时候使用，不分页
        /// </summary>
        /// <param name="strCondition"></param>
        /// <returns></returns>
        public DataSet GetList(string strCondition)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SLog_ID,SLog_Type,SLog_Content,SLog_Modu_ID,SLog_User_ID,SLog_Date,A.Comp_ID,A.CreateMan,A.CreateTime,A.State,Modu_Name , Empl_Name FROM SystemLog  A 
                                LEFT JOIN Module B ON (B.Modu_ID = A.SLog_Modu_ID) 
                                LEFT JOIN [User] C ON (C.[User_ID] = A.SLog_User_ID)
                                LEFT JOIN Employee D ON (D.Empl_ID =C.User_Empl_ID) "
                            );
#if DEBUG
                sbSql.Append("WHERE A.State = 0 ");
#else
                sbSql.Append("WHERE A.State = 0 AND SLog_Type=1");  
#endif
                if (strCondition != "")
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY CreateDate DESC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtSystemLog" });
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日志获取出错]:" + ex.ToString(), 0);
            }
            db.Close();
            return db.Data;
        }
        /// <summary>
        /// 清空当前企业的日志信息
        /// </summary>
        /// <returns></returns>
        public bool Delete()
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" DELETE FROM  SystemLog ");
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "操作日志清空完成！", EMenuList.操作日志);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "[日志清除出错！]" + ex.ToString(), 0);
            }
            db.Close();
            return blRes;
        }

    
    }

}
